package com.lk.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lk.entity.Student;
import com.lk.service.StudentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @类名: StudentController
 * @描述: (这里叙述类的作用 ， 使用方法 ， 行为等基本信息 ， 越详细约好 ）
 * @作者: LiuKai
 * @版本: 1.0
 * @创建时间: 2025/3/9 16:20
 * @修改历史: （列表如下）
 * 时间    修改人   修改原因  修改内容
 * XXX     XXXX      XXXXX    源文件那个方法的那个代码块
 */
@RestController
@RequestMapping("/students")
@Tag(name = "学生管理", description = "学生信息增删改查接口")
public class StudentController {

    @Autowired
    private StudentService studentService;
    @Operation(summary = "添加学生", description = "创建新的学生记录")
    @ApiResponse(responseCode = "200", description = "操作成功")
    @PostMapping
    public boolean addStudent(@RequestBody Student student) {
        return studentService.save(student);
    }

    @Operation(summary = "删除学生", description = "根据ID删除学生记录")
    @DeleteMapping("/{id}")
    public boolean deleteStudent(@PathVariable Long id) {
        return studentService.removeById(id);
    }

    @PutMapping
    public boolean updateStudent(@RequestBody Student student) {
        return studentService.updateById(student);
    }

    @GetMapping("/{id}")
    public Student getStudentById(@PathVariable Long id) {
        return studentService.getById(id);
    }

    @GetMapping
    public List<Student> getAllStudents() {
        return studentService.list();
    }

    @GetMapping("/search")
    public List<Student> searchStudents(@RequestParam String name) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);
        return studentService.list(queryWrapper);
    }
}
